<?php
/**
 * 业主首页
 * @author Administrator
 *
 */
class index extends CControllerOwnerLoginBase{
	
	public function show()
	{
		$type = $this->getRequestData("type","get",2);
		
		if($type!=1 && $type!=2)
		{
			$type = 2;
		}
		
		$obj = new integralChangeModel();
		$c = 0;
		$list = $obj->selectPage(1,10,"ic.shop_id,cost_time,cost_money_t,cost_shop_after_money,shop_name",
			"ic.shop_id=t.shop_id and owner_id=".$this->_ownerID." and cost_type=".$type,
			"c_id desc","",$c,"t_integral_change ic, t_shop t"
		);
		$this->render("index",array("type"=>$type,"data"=>$list));
	}
	 
	public function first_login()
	{
		$oModel = new ownerModel();
		
		$oInfo = $oModel->selectByWhere("t_owner",
				"supplementary_card,first_login,mail",
				array(),"del_flag=0 and owner_id=".$this->_ownerID);
		
		if(!$oInfo)
		{
			$this->logout();
		}
		
		if($oInfo["first_login"]==0)
		{
			$this->redirectUrl("/owner/index/show");
		}
		
		$rule = CCustomFormValidator::firstLoginValidator($this->_ownerID);
		$this->formValidator->init(array("formID" => "firstLoginForm", "alertErrorMsg" => false));
		$this->formValidator->setRules($rule);
		
		if ($_POST) {
			$_GET["id"]=$this->_ownerID;
			$error = $this->formValidator->run();
			if ($error) {
				echo "error";
				exit;
			}
			else {
				$email = $this->getRequestData("txt_email");
				$user_name = $this->getRequestData("txt_user_name");
				$user_pwd = $this->getRequestData("txt_user_pwd");
				$pay_pwd = $this->getRequestData("txt_pay_pwd");
		
				$user_pwd = CCommon::pwdMd5($user_pwd);
				$pay_pwd = CCommon::pwdMd5($pay_pwd);
		
				$ownerInfo["owner_id"] = $this->_ownerID;
				$ownerInfo["user_name"] = $user_name;
				$ownerInfo["mail"] = $email;
				$ownerInfo["user_pwd"] = $user_pwd;
				$ownerInfo["pay_pwd"] = $pay_pwd;
				$ownerInfo["first_login"] = 0;
		
				$r = $oModel->updateByPkId($ownerInfo);
				if($r)
				{
					$this->setJsBlock("alert('修改成功,点击确定进入后台');window.location.href='/owner/index/show';");
				}
				else{
					$this->setJsBlock("alert('修改失败,请重试');");
				}
			}
		}
		
		$this->render("first_login",array("supplementary_card"=>$oInfo["supplementary_card"],"mail"=>$oInfo["mail"]),false);
	}
	
	public function checkUserName()
	{
		$f = new CCustomFormValidator();
		echo $f->checkOwnerUserName($this->getRequestData("txt_user_name","get"))?1:0;
		exit;
	}
	
	/**
	 * 修改卡片状态
	 */
	public function op_status()
	{
		$type=$this->getRequestData("type");
		$pwd=$this->getRequestData("pwd");
		
		if($type!=1 && $type!=2)
		{
			echo -10;
			exit;
		}
		
		$pwd = CCommon::pwdMd5($pwd);
		
		$ownerModel = new ownerModel();
		if($ownerModel->checkPayPwdByOwner($this->_ownerID,$pwd))
		{
			if($ownerModel->changeCardStatus($this->_ownerID,$type))
			{
				echo 1;
			}
			else{
				echo -2; //修改失败
			}
		}
		else{
			echo -1; //密码不正确
		}
		
		exit;
	}
	
	public function changePwd()
	{
		$rule = CCustomFormValidator::changeOwnerPwdValidator();
		$this->formValidator->init(array("formID" => "pwdForm","validatorgroup"=>"pwdForm"));
		$this->formValidator->setRules($rule,"pwdForm");
		
		$rulePay = CCustomFormValidator::changeOwnerPayPwdValidator();
		$this->formValidator->init(array("formID" => "payPwdForm","validatorgroup"=>"payPwdForm"));
		$this->formValidator->setRules($rulePay,"payPwdForm");
		
		if ($_POST) {
			$pwdType = $this->getRequestData("hidType");
			if($pwdType!="pwdForm" && $pwdType!="payPwdForm")
			{
				echo "error";
				exit;
			}
			
			$error = $this->formValidator->run($pwdType);
			if ($error) {
				echo "error";
				exit;
			}
			else {
				$checkFun = "";
				
				if($pwdType=="pwdForm")
				{
					$old_pwd = CCommon::pwdMd5($this->getRequestData("txt_old_pwd"));
					$user_pwd = CCommon::pwdMd5($this->getRequestData("txt_user_pwd"));
					$checkFun = "checkUserPwd";
					$ownerInfo["user_pwd"] = $user_pwd;
				}
				else{
					$old_pwd = CCommon::pwdMd5($this->getRequestData("txt_old_pay_pwd"));
					$user_pwd = CCommon::pwdMd5($this->getRequestData("txt_pay_pwd"));
					$checkFun = "checkPayPwdByOwner";
					$ownerInfo["pay_pwd"] = $user_pwd;
				}
		
				$ownerInfo["owner_id"] = $this->_ownerID;
				
				$ownerModel = new ownerModel();
		
				if($ownerModel->$checkFun($this->_ownerID,$old_pwd))
				{
					$r = $ownerModel->updateByPkId($ownerInfo);
					if($r)
					{
						$this->setJsBlock("alert('修改成功');window.location.href='/owner/index/show';");
					}
					else{
						$this->setJsBlock("alert('修改失败,请重试');");
					}
				}
				else{
					$this->setJsBlock("alert('原密码错误!')");
				}
			}
		}
		$this->render("change_pwd");
	}
}